package com.cskaoyan.service.adminservice.impl;

import com.cskaoyan.bean.po.MarketCoupon;
import com.cskaoyan.bean.po.MarketCouponExample;
import com.cskaoyan.bean.po.MarketCouponUser;
import com.cskaoyan.bean.po.MarketCouponUserExample;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.mapper.MarketCouponMapper;
import com.cskaoyan.mapper.MarketCouponUserMapper;
import com.cskaoyan.service.adminservice.CouponsService;
import com.cskaoyan.util.RandomUtil;
import com.cskaoyan.util.StringUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;

/**
 * @author 小黑球
 * @datetime 2022/12/30 8:38
 * @description: TODO:
 */
@Service
public class CouponsServiceImpl implements CouponsService {

    @Autowired
    MarketCouponMapper marketCouponMapper;

    @Autowired
    MarketCouponUserMapper marketCouponUserMapper;

    @Override
    public CommonData<MarketCoupon> list(BasePageInfo info, String name, Short type, Short status) {
        // 分页
        PageHelper.startPage(info.getPage(), info.getLimit());
        MarketCouponExample example = new MarketCouponExample();
        // 排序
        example.setOrderByClause(info.getSort() + " " + info.getOrder());

        // 拼接条件
        MarketCouponExample.Criteria criteria = example.createCriteria();
        if (!StringUtil.isEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        if (type != null) {
            criteria.andTypeEqualTo(type);
        }
        if (status != null) {
            criteria.andStatusEqualTo(status);
        }
        criteria.andDeletedEqualTo(false);
        List<MarketCoupon> list = marketCouponMapper.selectByExample(example);

        // 获得pageInfo的最终目标是封装一个CommOnData
        PageInfo<MarketCoupon> pageInfo = new PageInfo<>(list);
        return CommonData.data(pageInfo);
    }

    @Override
    public void update(MarketCoupon marketCoupon) {
        marketCouponMapper.updateByPrimaryKeySelective(marketCoupon);
    }

    @Override
    public void delete(MarketCoupon marketCoupon) {
        marketCoupon.setDeleted(true);
        marketCouponMapper.updateByPrimaryKeySelective(marketCoupon);
    }

    @Override
    public CommonData<MarketCouponUser> listuser(BasePageInfo info, Integer couponId, Integer userId, Short status) {
        // 分页
        PageHelper.startPage(info.getPage(), info.getLimit());
        MarketCouponUserExample example = new MarketCouponUserExample();
        // 排序
        example.setOrderByClause(info.getSort() + " " + info.getOrder());

        // 拼接条件
        MarketCouponUserExample.Criteria criteria = example.createCriteria();
        if (couponId != null) {
            criteria.andCouponIdEqualTo(couponId);
        }
        if (userId != null) {
            criteria.andUserIdEqualTo(userId);
        }
        if (status != null) {
            criteria.andStatusEqualTo(status);
        }
        criteria.andDeletedEqualTo(false);
        List<MarketCouponUser> list = marketCouponUserMapper.selectByExample(example);

        // 封装一个CommonData
        PageInfo<MarketCouponUser> pageInfo = new PageInfo<>(list);
        return CommonData.data(pageInfo);
    }

    @Override
    public MarketCoupon read(Integer id) {
     /*   MarketCouponExample example = new MarketCouponExample();
        // 拼接条件
        MarketCouponExample.Criteria criteria = example.createCriteria();
        if (id != null) {
            criteria.andIdEqualTo(id);
        }
        // criteria.andDeletedEqualTo(false);
        List<MarketCoupon> list = marketCouponMapper.selectByExample(example);*/
        MarketCoupon marketCoupon = marketCouponMapper.selectByPrimaryKey(id);
        // 封装一个CommonData
//        PageInfo<MarketCoupon> pageInfo = new PageInfo<>(list);
        return marketCoupon;
    }

    @Override
    public MarketCoupon create(MarketCoupon marketCoupon) {
        marketCoupon.setAddTime(new Date());
        if(marketCoupon.getType() == 2){
            marketCoupon.setCode(RandomUtil.couponCode());
        }
        marketCouponMapper.insertSelective(marketCoupon);
        return marketCoupon;
    }
}
